package com.example.weight.captcha;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.weight.R;

public class PicSeekBar extends LinearLayout {
    private ImageView mThumbView;
    private TextView mDescriptionView;
    private RelativeLayout.LayoutParams resetThumbParam;
    private float progress;
    private PicSeekBar picSeekBar;
    private int moveWidth; //滑动区间

    public PicSeekBar(Context context) {
        super(context);
        initView();
    }

    public PicSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private float downPoint = 0;

    private void initView() {
        picSeekBar = this;
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_pick_bar, this, true);
        mThumbView = view.findViewById(R.id.iv_thumb);
        mDescriptionView = view.findViewById(R.id.tv_description);

        resetThumbParam = (RelativeLayout.LayoutParams) mThumbView.getLayoutParams();
        mThumbView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(moveWidth == 0){//滑动距离，在初始化的时候可能为0，但是在控件绘制之后点击时就一定能拿到值，所以在此初始化
                    moveWidth = getWidth() - mThumbView.getWidth();
                }
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        downPoint = event.getRawX();
                        if (mOnSeekBarChangeListener != null) {
                            mOnSeekBarChangeListener.onStartTrackingTouch(picSeekBar);
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if ((event.getRawX() - downPoint) > (moveWidth)) {
                            progress = 100;
                        } else if ((event.getRawX() - downPoint) < 0 && mThumbView.getTranslationX() == 0) {
                            progress = 0;
                        } else {
                            progress = (event.getRawX() - downPoint) * 100 / (moveWidth);
                        }
                        invalidateProgress(true);
                        if (mOnSeekBarChangeListener != null) {
                            mOnSeekBarChangeListener.onProgressChanged(picSeekBar, (int) progress, true);
                        }
                        break;
                    case MotionEvent.ACTION_CANCEL:
                    case MotionEvent.ACTION_UP:
                        if (mOnSeekBarChangeListener != null) {
                            mOnSeekBarChangeListener.onStopTrackingTouch(picSeekBar);
                            downPoint = 0;
                        }
                        break;
                }
                return true;
            }
        });
    }

    public void setProgress(int progress) {
        this.progress = progress;
        invalidateProgress(false);
    }

    public int getProgress() {
        return (int) progress;
    }

    public void invalidateProgress(boolean fromUser) {
        RelativeLayout.LayoutParams params = resetThumbParam;
        params.leftMargin = (int) (progress * moveWidth / 100);
        mThumbView.setLayoutParams(params);
        if (mOnSeekBarChangeListener != null) {
            mOnSeekBarChangeListener.onProgressChanged(picSeekBar, (int) progress, fromUser);
        }
    }

    private OnSeekBarChangeListener mOnSeekBarChangeListener;

    public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
        mOnSeekBarChangeListener = l;
    }

    public interface OnSeekBarChangeListener {

        /**
         * Notification that the progress level has changed. Clients can use the fromUser parameter
         * to distinguish user-initiated changes from those that occurred programmatically.
         *
         * @param seekBar  The SeekBar whose progress has changed
         * @param progress The current progress level. This will be in the range min..max where min
         *                 and max were set by respectively. (The default values for
         *                 min is 0 and max is 100.)
         * @param fromUser True if the progress change was initiated by the user.
         */
        void onProgressChanged(PicSeekBar seekBar, int progress, boolean fromUser);

        /**
         * Notification that the user has started a touch gesture. Clients may want to use this
         * to disable advancing the seekbar.
         *
         * @param seekBar The SeekBar in which the touch gesture began
         */
        void onStartTrackingTouch(PicSeekBar seekBar);

        /**
         * Notification that the user has finished a touch gesture. Clients may want to use this
         * to re-enable advancing the seekbar.
         *
         * @param seekBar The SeekBar in which the touch gesture began
         */
        void onStopTrackingTouch(PicSeekBar seekBar);
    }
}
